package com.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dao.IBisOpeDao;
import com.dao.IBisPathDao;
import com.dao.IBisPathiDao;
import com.dao.IRetPrdOutDao;
import com.def.GenericDef;
import com.model.Bis_mdl_def;
import com.model.Bis_ope;
import com.model.Bis_opeId;
import com.model.Bis_path;
import com.model.Bis_pathId;
import com.model.Bis_pathi;
import com.model.Bis_pathiId;
import com.model.Ret_prd_info;
import com.model.Ret_prd_out;
import com.model.Ret_prd_outId;
import com.rtnBean.logoff.CrOpeAll;
import com.rtnBean.logoff.NxOpeAll;
import com.tx.Xpbisope.XpbisopeI;
import com.tx.Xpbisope.XpbisopeIA;
import com.tx.Xpbisope.XpbisopeO;
import com.tx.Xpbisope.XpbisopeOA;
import com.util.Util;

import static com.def.GenericDef.*;

@Repository
public class BisOpeHibernateDao extends GenericHibernateDao<Bis_ope, Bis_opeId>
		implements IBisOpeDao {

	@Autowired
	private IBisPathiDao bisPathiDao;

	@Autowired
	private IBisPathDao bisPathDao;
	
	@Autowired
	private IRetPrdOutDao retPrdOut;


	public CrOpeAll getCrOpeInfo(String crPathID, String crPathVer,
			String crOpeNO) {
		CrOpeAll crOpeAll = new CrOpeAll();
		Bis_pathi crPathi = new Bis_pathi();
		Bis_path crPath = new Bis_path();
		Bis_ope crOpe = new Bis_ope();

		Bis_pathiId bisPathiId = new Bis_pathiId();
		bisPathiId.setPath_id(crPathID);
		bisPathiId.setPath_ver(crPathVer);

		Bis_pathId bisPathId = new Bis_pathId();
		bisPathId.setPath_id_fk(crPathID);
		bisPathId.setPath_ver_fk(crPathVer);
		bisPathId.setCr_ope_no(crOpeNO);

		crPathi = bisPathiDao.get(bisPathiId);
		crPath = bisPathDao.get(bisPathId);

		Bis_opeId bisOpeId = new Bis_opeId();
		bisOpeId.setOpe_id(crPath.getCr_ope_id_fk());
		bisOpeId.setOpe_ver(crPath.getCr_ope_ver_fk());

		crOpe = this.get(bisOpeId);

		crOpeAll.setCrPathi(crPathi);
		crOpeAll.setCrPath(crPath);
		crOpeAll.setCrOpe(crOpe);

		return crOpeAll;
	}


	public NxOpeAll getNxOpeInfo(String pathID, String pathVer, String crOpeNO) {
		NxOpeAll nxOpeAll = new NxOpeAll();

		Bis_pathi pathi = new Bis_pathi();
		Bis_path path = new Bis_path();
		Bis_path nxPath = new Bis_path();
		Bis_ope nxOpe = new Bis_ope();

		Bis_pathiId bisPathiId = new Bis_pathiId();
		Bis_pathId bisPathId = new Bis_pathId();
		Bis_pathId nxBisPathId = new Bis_pathId();
		Bis_opeId bisOpeId = new Bis_opeId();
		
		bisPathiId.setPath_id(pathID);
		bisPathiId.setPath_ver(pathVer);
		pathi = bisPathiDao.get(bisPathiId);

		bisPathId.setPath_id_fk(pathID);
		bisPathId.setPath_ver_fk(pathVer);
		bisPathId.setCr_ope_no(crOpeNO);
		path = bisPathDao.get(bisPathId);
		if (_SPACE.equals(path.getNx_ope_no()) ||(_SPACE0.equals(path.getNx_ope_no()))|| (null == path.getNx_ope_no())) {
			nxPath = null;
			nxOpe = null;
		}else{
			nxBisPathId.setPath_id_fk(pathID);
			nxBisPathId.setPath_ver_fk(pathVer);
			nxBisPathId.setCr_ope_no(path.getNx_ope_no());
			nxPath = bisPathDao.get(nxBisPathId);
	
			bisOpeId.setOpe_id(nxPath.getCr_ope_id_fk());
			bisOpeId.setOpe_ver(nxPath.getCr_ope_ver_fk());
			nxOpe = this.get(bisOpeId);
		}
		nxOpeAll.setNxPathi(pathi);
		nxOpeAll.setNxPath(nxPath);
		nxOpeAll.setNxOpe(nxOpe);
		
		return nxOpeAll;
	}

	public XpbisopeO queryByCondition(XpbisopeI xpbisopeI){
		XpbisopeO xpbisopeO = new XpbisopeO();
		xpbisopeO.setTrx_id("XPBISOPE");
		xpbisopeO.setType_id("O");
		XpbisopeIA iary = xpbisopeI.getIary().get(0);
		/***
		 * 支持 按ope_id,ope_ver 模糊查询
		 * ***/
		String ope_id  = iary.getOpe_id();
		String ope_ver = iary.getOpe_ver();
		String proc_id = iary.getProc_id();
//		String hql ="FROM Bis_ope where ope_id=:ope_id and ope_ver=:ope_ver ";
//		Query query = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
//		query.setString("ope_id",ope_id);
//		query.setString("ope_ver",ope_ver);
//		Bis_ope bisOpe =(Bis_ope) query.uniqueResult();
		String hql ="FROM Bis_ope where 1=1 ";
		if(ope_id!=null){
			if(ope_id.indexOf('*')==-1){
				hql = hql + " and ope_id='" + ope_id + "'";
			}else{
				hql = hql + " and ope_id like '" + ope_id.replace('*', '%') + "'";
			}
		}
		if(ope_ver!=null){
			if(ope_ver.indexOf('*')==-1){
				hql = hql + " and ope_ver='" + ope_ver + "'";
			}else{
				hql = hql + " and ope_ver like '" + ope_ver.replace('*', '%') + "'";
			}
		}
		if(proc_id!=null){
//			hql = hql + " and proc_id like '%" + proc_id + "%'";
			hql = hql + " and proc_id in (SELECT data_ext FROM Bis_data where data_cate = 'PRST' " +
						" and ext_4 =  '" + proc_id + "')"; 
		}
		hql = hql + " order by convert(pep_lvl,UNSIGNED)";
		List<Bis_ope> bisOpeList = this.find(hql);
//		if(bisOpeList.size()==0){
//			xpbisopeO.setRtn_code(E_XPBISOPE_NOT_FOUND_OPE +"");
//			xpbisopeO.setRtn_mesg("not found record ");
//			return xpbisopeO;
//			
//		}
		List<XpbisopeOA> oaryList = new ArrayList<XpbisopeOA>();
		for(Bis_ope bis_ope : bisOpeList){
			XpbisopeOA oary = new XpbisopeOA();
			oary.setOpe_id(bis_ope.getId().getOpe_id());
			oary.setOpe_ver(bis_ope.getId().getOpe_ver());
			oary.setOpe_dsc(bis_ope.getOpe_dsc());
			oary.setProc_id(bis_ope.getProc_id());
			oary.setToolg_id(bis_ope.getToolg_id());
	        oary.setPep_lvl(bis_ope.getPep_lvl());
	        oary.setSwh_seq_id(bis_ope.getSwh_seq_id());
	        oary.setSwh_cnt(bis_ope.getSwh_cnt());
	        oary.setYield_chk_flg(bis_ope.getYield_chk_flg());
	        oary.setHand_over_flg(bis_ope.getHand_over_flg());
	        oary.setDept_id_data_id_fk(bis_ope.getDept_id_data_id_fk());
	        oary.setStd_ope_time(bis_ope.getStd_ope_time());
	        oary.setUp_load_id(bis_ope.getUp_load_id());
	        oary.setDn_load_id(bis_ope.getDn_load_id());
	        oaryList.add(oary);
		}
        xpbisopeO.setOary(oaryList);
        xpbisopeO.setTbl_cnt(oaryList.size());
        xpbisopeO.setRtn_code(RETURN_CODE_OK);
        xpbisopeO.setRtn_mesg("success");
		return xpbisopeO;
	}
	public XpbisopeO addProc(XpbisopeI xpbisopeI){
		XpbisopeO xpbisopeO = new XpbisopeO();
		xpbisopeO.setTrx_id("XPBISOPE");
		xpbisopeO.setType_id("O");
		XpbisopeIA iary = xpbisopeI.getIary().get(0);
		String ope_id = iary.getOpe_id();
		String ope_ver = iary.getOpe_ver();
		Bis_ope queryBisOpe = getDataByKeyProc(ope_id,ope_ver);
		if(queryBisOpe!=null){
			xpbisopeO.setRtn_code(E_XPBISOPE_OPE_ALREADY_EXIST+"");
			xpbisopeO.setRtn_code("ope_id Already Exist");
			return xpbisopeO;
		}
		try{
			Bis_ope bisOpe = new Bis_ope();
			Bis_opeId id = new Bis_opeId();
			id.setOpe_id(ope_id);
			id.setOpe_ver(ope_ver);
			bisOpe.setId(id);
		    bisOpe.setOpe_dsc(iary.getOpe_dsc());
			bisOpe.setProc_id(iary.getProc_id());
			bisOpe.setToolg_id(iary.getToolg_id());
			bisOpe.setPep_lvl(iary.getPep_lvl());
	        bisOpe.setSwh_seq_id(bisOpe.getSwh_seq_id());
	        bisOpe.setSwh_cnt(bisOpe.getSwh_cnt());
	        bisOpe.setYield_chk_flg(iary.getYield_chk_flg());
	        bisOpe.setHand_over_flg(iary.getHand_over_flg());
	        bisOpe.setDept_id_data_id_fk(iary.getDept_id_data_id_fk());
	        bisOpe.setStd_ope_time(iary.getStd_ope_time());
	        bisOpe.setUp_load_id(iary.getUp_load_id());
	        bisOpe.setDn_load_id(iary.getDn_load_id());
	        this.save(bisOpe);
		}catch(Exception ex){
			logger.error(Util.stackTraceToString(ex));
		}
		
        xpbisopeO.setRtn_code(RETURN_CODE_OK);
        xpbisopeO.setRtn_mesg("success");
		return xpbisopeO;
	}
	public XpbisopeO updateProc(XpbisopeI xpbisopeI){
		XpbisopeO xpbisopeO = new XpbisopeO();
		xpbisopeO.setTrx_id("XPBISOPE");
		xpbisopeO.setType_id("O");
		XpbisopeIA iary = xpbisopeI.getIary().get(0);
		String ope_id = iary.getOpe_id();
		String ope_ver = iary.getOpe_ver();
		Bis_ope bisOpe = getDataByKeyProc(ope_id,ope_ver);
		if(bisOpe==null){
			xpbisopeO.setRtn_code(E_XPBISOPE_NOT_FOUND_OPE+"");
			xpbisopeO.setRtn_code("not found ope");
			return xpbisopeO;
		}
		if(iary.getOpe_dsc()!=null){
			bisOpe.setOpe_dsc(iary.getOpe_dsc());
		}
	    if(iary.getProc_id()!=null){
	    	bisOpe.setProc_id(iary.getProc_id());
	    }
		if(iary.getToolg_id()!=null){
			bisOpe.setToolg_id(iary.getToolg_id());
		}
		if(iary.getPep_lvl()!=null){
			bisOpe.setPep_lvl(iary.getPep_lvl());
		}
		if(bisOpe.getSwh_seq_id()!=null){
			bisOpe.setSwh_seq_id(bisOpe.getSwh_seq_id());
		}
        if(bisOpe.getSwh_cnt()!=null){
        	bisOpe.setSwh_cnt(bisOpe.getSwh_cnt());
        }
        if(iary.getYield_chk_flg()!=null){
        	bisOpe.setYield_chk_flg(iary.getYield_chk_flg());
        }
        if(iary.getHand_over_flg()!=null){
        	bisOpe.setHand_over_flg(iary.getHand_over_flg());
        }
        if(iary.getDept_id_data_id_fk()!=null){
        	bisOpe.setDept_id_data_id_fk(iary.getDept_id_data_id_fk());
        }
        if(iary.getStd_ope_time()!=null){
        	bisOpe.setStd_ope_time(iary.getStd_ope_time());
        }
        if(iary.getUp_load_id()!=null){
        	bisOpe.setUp_load_id(iary.getUp_load_id());
        }
        if(iary.getDn_load_id()!=null){
        	bisOpe.setDn_load_id(iary.getDn_load_id());
        }
        this.update(bisOpe);
        xpbisopeO.setRtn_code(RETURN_CODE_OK);
        xpbisopeO.setRtn_mesg("success");
		return xpbisopeO;
	}
	public XpbisopeO deleteProc(XpbisopeI xpbisopeI){
		XpbisopeO xpbisopeO = new XpbisopeO();
		xpbisopeO.setTrx_id("XPBISOPE");
		xpbisopeO.setType_id("O");
		XpbisopeIA iary = xpbisopeI.getIary().get(0);
		String ope_id = iary.getOpe_id();
		String ope_ver = iary.getOpe_ver();
		Bis_ope bisOpe = getDataByKeyProc(ope_id,ope_ver);
		if(bisOpe==null){
			xpbisopeO.setRtn_code(E_XPBISOPE_NOT_FOUND_OPE+"");
			xpbisopeO.setRtn_code("not found ope");
			return xpbisopeO;
		}
		this.delete(bisOpe);
        xpbisopeO.setRtn_code(RETURN_CODE_OK);
        xpbisopeO.setRtn_mesg("success");
		return xpbisopeO;
	}
	public Bis_ope getDataByKeyProc(String ope_id,String ope_ver){
		String hql = "FROM Bis_ope where ope_id=:ope_id and ope_ver=:ope_ver";
		Query query = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		query.setString("ope_id",ope_id);
		query.setString("ope_ver",ope_ver);
		Bis_ope bisOpe = (Bis_ope) query.uniqueResult();
		return bisOpe;
	}
	public XpbisopeO listAllFnc(XpbisopeI xpbisopeI){
		XpbisopeO xpbisopeO = new XpbisopeO();
		xpbisopeO.setTrx_id("XPBISOPE");
		xpbisopeO.setType_id("O");
		List<XpbisopeOA> oaryList = new ArrayList<XpbisopeOA>();
		String hql = "FROM Bis_ope where 1=1 order by convert(pep_lvl,UNSIGNED) ";
		List<Bis_ope> bisOpeList = this.find(hql);
		for(Bis_ope bis_ope:bisOpeList){
			XpbisopeOA oary = new XpbisopeOA();
			oary.setOpe_id(bis_ope.getId().getOpe_id());
			oary.setOpe_ver(bis_ope.getId().getOpe_ver());
			oary.setOpe_dsc(bis_ope.getOpe_dsc());
			oary.setProc_id(bis_ope.getProc_id());
			oaryList.add(oary);
		}
		xpbisopeO.setOary(oaryList);
		xpbisopeO.setTbl_cnt(oaryList.size());
		xpbisopeO.setRtn_code(RETURN_CODE_OK);
		xpbisopeO.setRtn_mesg("success");
		return xpbisopeO;
	}
}
